#include "aabb.h"

#include "ray.h"

namespace venus {
bool AABB::Intersect( const Ray& ray ) const { 
  float t0 = ray.tmin, t1 = ray.tmax;
  for (int axis = 0; axis < 3; ++axis) {
    float tnear = (minP[axis] - ray.o[axis]) * ray.inv_d[axis];
    float tfar = (maxP[axis] - ray.o[axis]) * ray.inv_d[axis];
    
    if (tnear > tfar) std::swap(tnear, tfar);
    t0 = tnear > t0 ? tnear : t0;
    t1 = tfar < t1 ? tfar : t1;
    if (t0 > t1) return false;
  }
  ray.tmin = t0; ray.tmax = t1;
  return true;
}
}//namespace venus
